/*
 * EP-Tracing System : a tool to trace mountain hike.           .kNNx
 * Copyright (C) 2009  Raffaello Brondi                         'KNNNk
 *                                                             ,XNNNl
 * This program is free software:                             ,XNN0'
 * you can redistribute it and/or modify                     ,XNNx.
 * it under the terms of the                                'XNNl
 * GNU General Public License                              .KNN:                     ..
 * as published by                                        .0NNo                    .ONNO.
 * the Free Software Foundation,                          xNNN.  :dkd,            .KNNNN;
 * either version 3 of the License, or                   :NNNK ;XNNNNNo          ,KNNNX:
 * (at your option) any later version.                   xNNNNcXNNNNNNk         cNNNNx.
 *                                                       ,XNNNNNNNNNNX'       .xNNNK;
 *                                                        .lKNNNNNNNK'       :KNNNo
 *                                                          ;kNNNNNKxl;'.'l0NNNk.
 * This program is distributed in                          .'  .:xXNNNNNNNNNNNO,
 * the hope that it will be useful,                       :NNXd'   ,oONNNNNNk,
 * but WITHOUT ANY WARRANTY;                              xNNNNN0c.    .;;,.
 * without even the implied warranty of                   kNNNNNNNXd'
 * MERCHANTABILITY or                                     xNNNNNNNNNNOc.
 * FITNESS FOR A PARTICULAR PURPOSE.                      xNNNNNNNNNNNNXd.
 * See the GNU General Public License                     ONNNN0.,o0NNNNNNl
 * for more details.                                      0NNNNd    kNNNNNX.
 *                                                        KNNNNl   oNNNNNXc
 *                                                       .XNNNN:  dNNNNNx.
 * You should have received a copy                       .XNNNN; lNNNNO'
 * of the GNU General Public License                     .XNNNN; 'dxo'
 * along with this program.                              .XNNNN;
 * If not, see <http://www.gnu.org/licenses/>.            0NNNN:
 *                                                        kNNNNc
 *                                                        oNNNNd
 *                                                        ;NNNN0
 * Contact: rbrondi@gmail.com                             .XNNNN;
 *                                                         kNNNNd
 *                                                         .coo:.
 */
package EP;

import EP.UI.Messages;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import org.json.me.JSONException;
import org.json.me.JSONObject;

/**
 *
 * @author Raffaello Brondi
 */
public class EPHReader {

    private final static int BUFFER_SIZE = 1024;
    private JSONObject jObj;

    public static EPHReader getInstance(String file) {
        EPHReader eph = null;
        try {
            FileConnection fc = (FileConnection) Connector.open("file://" + file);

            if (!fc.exists()) {
                throw new IOException("File does not exists");
            }

            InputStream fis = fc.openInputStream();
            byte[] buffer = new byte[BUFFER_SIZE];
            int length = 0;
            while (length != -1) {
                length = fis.read(buffer, 0, BUFFER_SIZE);
            }
            fis.close();
            fc.close();

            //System.out.println(new String(buffer));
            eph = new EPHReader(new JSONObject(new String(buffer)));
        } catch (JSONException ex) {
            ex.printStackTrace();
            Messages.showNotWellFormedEPH();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return eph;
    }

    private EPHReader(JSONObject obj) {
        this.jObj = obj;
    }

    public String get(String key) {
        String result = null;
        try {
            result = this.jObj.getString(key);
        } catch (JSONException ex) {
            ex.printStackTrace();
            Messages.showNotWellFormedEPH();
        }
        return result;
    }
    /*public String getName() {
        String name = null;
        try {
            name = this.jObj.getString(Utils.HIKE_NAME);
        } catch (JSONException ex) {
            ex.printStackTrace();
            Messages.showNotWellFormedEPH();
        }
        return name;
    }

    public String getDistance() {
        String dist = null;
        try {
            dist = this.jObj.getString(Utils.HIKE_TOTAL_DISTANCE);
        } catch (JSONException ex) {
            ex.printStackTrace();
            Messages.showNotWellFormedEPH();
        }
        return dist;
    }
    public String getTime() {
        String time = null;
        try {
            time = this.jObj.getString(Utils.HIKE_TOTAL_TIME);
        } catch (JSONException ex) {
            ex.printStackTrace();
            Messages.showNotWellFormedEPH();
        }
        return time;
    }
    public String getAltitude() {
        String alt = null;
        try {
            alt = this.jObj.getString(Utils.HIKE_ALTITUDE);
        } catch (JSONException ex) {
            ex.printStackTrace();
            Messages.showNotWellFormedEPH();
        }
        return alt;
    }*/
}
